package com.wuuxiang.polestar.demo.springboot;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import com.google.common.collect.Maps;
import com.wuuxiang.polestar.core.GeneralConfigGroup;
import com.wuuxiang.polestar.demo.util.ZookeeperServer;

/**********************************************************
 * 类名称：SpringBootDemo 	<br>
 * 类描述：   				<br>
 * 创建人：liuwei 			<br>
 * 创建时间：2018年1月5日上午9:49:01	<br>
 * 修改备注：   				<br>
 **********************************************************/
@SpringBootApplication
@ComponentScan(basePackages = "com.wuuxiang.polestar.demo.springboot")
public class SpringBootDemo {

	static {
		try {
			int port = 2180;
			ZookeeperServer.start(port);
			Map<String, String> data = Maps.newHashMap();
			data.put("/polestar/config/crm6/comm-conf/test1", "1");
			data.put("/polestar/config/crm6/comm-conf/test2", "2");
			data.put("/polestar/config/crm6/comm-conf/test3", "3");
			data.put("/polestar/config/crm6/comm-conf/test4", "4");
			data.put("/polestar/config/crm6/comm-conf/defaultTest", "666");
			data.put("/polestar/config/default/comm-conf/defaultTest", "5");
			CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:" + port,
					new ExponentialBackoffRetry(100, 2));
			client.start();
			for (Entry<String, String> item : data.entrySet()) {
				Stat stat = client.checkExists().forPath(item.getKey());
				if (stat == null) {
					client.create().creatingParentsIfNeeded().forPath(item.getKey(), item.getValue().getBytes());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(SpringBootDemo.class);
		ApplicationContext ctx = app.run(args);
		GeneralConfigGroup bean = (GeneralConfigGroup) ctx.getBean("generalConfigGroup");
		System.out.println(bean.get("test4"));
	}
}
